<?php
class Category_LabelController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('main');

        $this->_model = new Category_Model_Label();

        $this->_authService = new Main_Service_Authentication();

        // add forms
        $this->view->labelform = $this->getLabelForm();
    }

    /**
     * shows the list of all the labels for a sub-catagories
     */
    public function indexAction()
    {
         // checking if user is logged in
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');
        
        $cat_model =  new Category_Model_Category();
        $this->view->categories = $cat_model->getAllCategories();

        $this->view->labels = $this->_model->getAllLabels();
    }

    public function newAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $this->_helper->FlashMessenger('')->clearMessages();
        $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('New Subcategory Label');
        $this->_forward('edit');
    }


    public function editAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

        $requst_params = $this->getRequest()->getParams();

        if( isset($requst_params['id']) && $requst_params['id'] !='' )
        {
            $this->_helper->FlashMessenger('')->clearMessages();
            $this->_helper->FlashMessenger('')->setNamespace('header')->addMessage('Edit Subcategory Label');
            $id  =  $requst_params['id'];
            $this->view->label = $this->_model->getLabelById($id);

            $this->view->labelform = $this->getLabelForm()->populate( $this->view->label->toArray() );
        }
    }

    public function saveAction()
    {
        $request = $this->getRequest();
        if (!$request->isPost()) {
            return $this->_helper->redirector('index');
        }
        $logger = Zend_Registry::get('log');
        $logger->log(print_r($request->getPost() ), Zend_Log::INFO );
        if (false === $this->_model->saveLabel($request->getPost())  )
        {
            $this->_helper->FlashMessenger('')->setNamespace('error')->addMessage('An error Occured');
            return $this->render('edit');
        }

        $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Subcategory Label saved successfuly !');
        return $this->_redirect('/category/label/index');
    }
    

    public function getSubcategoryAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->viewRenderer('ajax');
        $this->_helper->layout()->setLayout('nolayout');
        $requst_params = $this->getRequest()->getParams();

        if(!$this->_request->isXmlHttpRequest())
            return;

        if($_REQUEST['catid'] != '' )
        {
            $subcatmodel = new Category_Model_Subcategory();
            $subcats = $subcatmodel->getSubcategoryArrayByParentId($_REQUEST['catid']);

            $this->getResponse()->setBody(json_encode($subcatmodel->getSubcategoryArrayByParentId($_REQUEST['catid'])) );
        }
    }

    /**
     * use to reurn the array of labels matching to the catid & subcatid in request params
     * @return json string
     */
    public function getLabeldataAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->viewRenderer('ajax');
        $this->_helper->layout()->setLayout('nolayout');
        $requst_params = $this->getRequest()->getParams();

        if(!$this->_request->isXmlHttpRequest())
            return;

        if($_REQUEST['catid'] != '' &&  $_REQUEST['subcatid'] != '')
        {
            $this->getResponse()->setBody(json_encode($this->_model->getLabelDataArray($_REQUEST['catid'], $_REQUEST['subcatid'] )) );
        }
    }

    public function deleteAction()
    {
        if(!$this->_authService->getAuth()->hasIdentity())
            $this->_redirect('/');

         if (false === ($id = $this->_getParam('id',false))) {
            throw new WOW_Exception('Unknown Label');
        }

        if($this->_model->deleteLabel($id))
            $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Subcategory Label Deleted');
        else
            $this->_helper->FlashMessenger('')->setNamespace('Error')->addMessage('An error occured');

        return $this->_redirect('/manage-labels');

    }

    public function getLabelForm()
    {
        return $this->_forms['labelform'] = $this->_model->getForm('LabelsLabelform');
    }
}
?>